package com.gxa.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.JsonResult;
import com.gxa.common.commons.R;
import com.gxa.finance.mapper.BillMapper;
import com.gxa.finance.pojo.Bill;
import com.gxa.finance.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.sql.Wrapper;
import java.util.List;

@Service
public class BillServiceImpl extends ServiceImpl<BillMapper,Bill> implements BillService {
    @Autowired
    private BillMapper billMapper;
    /**
     * 创建收款计划
     * @param bill
     * @return
     */
    @Override
    public R addBill(Bill bill) {
        R r = new R();

        if ("收款".equals(bill.getbType())){
            String bill_id = bill.getOrderId()+"-S"+"01";
            bill.setBillId(bill_id);

        }else if ("付款".equals(bill.getbType())){
            String bill_id = bill.getOrderId()+"-F"+"01";
            bill.setBillId(bill_id);
        }
        bill.setCreateTime(new Timestamp(System.currentTimeMillis()));

        try {
            int result = billMapper.insert(bill);
            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("添加失败");
        }
        return r;
    }

    @Override
    public R updateBillStatus(Bill bill) {
        R r = new R();
        try {
            bill.setPaymentTime(new Timestamp(System.currentTimeMillis()));
            BigDecimal subtract = bill.getMoney().subtract(bill.getyMoney());
            bill.setwMoney(subtract);
            int result = billMapper.updateById(bill);
            if (result>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("修改失败");
        }
        return r;
    }

    @Override
    public JsonResult findByDateBills(Integer currentPage, Integer pageSize, String staTime, String offTime,String b_type,Integer b_status) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer  sta = (currentPage-1)*pageSize;
            Integer  off = pageSize;
            int count = billMapper.findByTimeCount(staTime, offTime,b_type,b_status);
            jsonResult.setCount(count);
            List<Bill>  billList = billMapper.findByDateBills(sta,off,staTime,offTime,b_type,b_status);
            jsonResult.setData(billList);
            if (count>0&& billList!=null){
                jsonResult.setCode(200);
                jsonResult.setMsg("查询成功");
            }
        }catch (Exception e){
            jsonResult.setCode(500);
            jsonResult.setMsg("查询失败");
        }

        return jsonResult;
    }

    @Override
    public JsonResult findBillList(Integer currentPage, Integer pageSize, String b_type) {
        JsonResult jsonResult = new  JsonResult();
        try {
            QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(b_type!=null&& !b_type.isEmpty(),"b_type",b_type);
            Page<Bill> page = new Page<>(currentPage,pageSize);
            Page<Bill> billList = super.page(page,queryWrapper);
            jsonResult.setCode(200);
            jsonResult.setMsg("查询成功");
            jsonResult.setData(billList.getRecords());
            jsonResult.setCount((int)billList.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    @Override
    public R findByOrderId(String orderId, String b_type, Integer b_status) {
        R r = new R();
        try {
            QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(orderId!=null&&!orderId.isEmpty(),"order_id",orderId)
                        .eq(b_type!=null&&!b_type.isEmpty(),"b_type",b_type)
                        .eq(b_status!=null,"b_status",b_status);
            List<Bill> billList = billMapper.selectList(queryWrapper);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(billList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("查询失败");
        }
        return r;
    }

    @Override
    public R findByBillId(String billId, String b_type, Integer b_status) {
        R r = new R();
        try {
            QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(billId!=null&&!billId.isEmpty(),"bill_id",billId)
                    .eq(b_type!=null&&!b_type.isEmpty(),"b_type",b_type)
                    .eq(b_status!=null,"b_status",b_status);
            List<Bill> billList = billMapper.selectList(queryWrapper);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(billList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("查询失败");
        }
        return r;
    }
}
